// api/user_api/user_list.go
package user_api

import (
	"github.com/gin-gonic/gin"
	"muziai-server/models"
	"muziai-server/models/ctype"
	"muziai-server/service/common"
	"muziai-server/utils/res"
)

type UserListRequest struct {
	models.Page
	Role ctype.Role `form:"role"`
}

type UserListResponse struct {
	models.UserModel
	UserCreateAIRoleCount int `json:"userCreateAIRoleCount"`
	UserSessionCount      int `json:"userSessionCount"`
	UserChatCount         int `json:"userChatCount"`
}

func (UserApi) UserListView(c *gin.Context) {

	// limit = 10
	// page = 1
	// sql  page  limit    offset
	//       1     10        0
	//       2     10       10
	//       3     10       20
	//       n     10    limit * (n-1)

	var cr UserListRequest
	c.ShouldBindQuery(&cr)
	var count int64

	_list, count, _ := common.QueryList(models.UserModel{
		Role: cr.Role,
	}, common.Option{
		Page:  cr.Page,
		Likes: []string{"nickname", "username"},
		Debug: true,
		Sort:  "created_at desc",
		Preloads: []string{
			"AiRoleList",
			"SessionList",
			"ChatList",
			"OrderList"},
	})
	var list = make([]UserListResponse, 0)
	for _, model := range _list {

		list = append(list, UserListResponse{
			UserModel:             model,
			UserCreateAIRoleCount: len(model.AiRoleList),
			UserSessionCount:      len(model.SessionList),
			UserChatCount:         len(model.ChatList),
		})
	}

	res.OkWithList(c, list, count)
	return

}
